﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using DevExpress.XtraCharts;

namespace NotebookS2.Reports.Carousel
{
    public partial class ProductMovement : DevExpress.XtraEditors.XtraForm
    {
        public ProductMovement()
        {
            InitializeComponent();
            
        }

        public string ConnectionString { get; set; }
        public string ItemSelected { get; set; }
        public string selectedWhs { get; set; }

        public void BindPhysicalCount()
        {
            SqlConnection oConnection = new SqlConnection(this.ConnectionString);
            oConnection.Open();

            SqlCommand oCommand = new SqlCommand("SELECT * FROM OPHC WHERE WhseCode = @Whs", oConnection);
            oCommand.Parameters.AddWithValue("@Whs", cmbWhs.SelectedValue);

            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            int iRow = dt.Rows.Count;

            if (iRow > 0)
            {
                btnDocument.DataSource = dt;
                btnDocument.DisplayMember = "DocNum";
                btnDocument.ValueMember = "DocNum";
                btnDocument.Refresh();
            }
        }

        public void BindWarehouse()
        {
            #region
            
            SqlConnection oConnection = new SqlConnection(this.ConnectionString);
            oConnection.Open();

            SqlCommand oCommand = new SqlCommand("SELECT * FROM WAREHOUSE WHERE DocStatus = @Status",oConnection);
            oCommand.Parameters.AddWithValue("@Status", "Active");

            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            int iRow = dt.Rows.Count;

            if (iRow > 0)
            {
                cmbWhs.DataSource = dt;
                cmbWhs.ValueMember = "Code";
                cmbWhs.DisplayMember = "Dscription";
                cmbWhs.Refresh();

                oCommand.Dispose();
                oConnection.Close();
            }
            #endregion
        }

      

        public void BindLotNumber()
        {
            #region

            if (btnItemCode.Text != "" && btnItemCode.Text != null)
            {
                SqlConnection oConnection = new SqlConnection(this.ConnectionString);
                oConnection.Open();

                SqlCommand oCommand = new SqlCommand("select  distinct(LotNumber) from stockcard where itemcode = @ItemCode", oConnection);
                oCommand.Parameters.AddWithValue("@ItemCode", btnItemCode.Text);

                SqlDataAdapter oAdapter = new SqlDataAdapter();
                DataTable dt = new DataTable();

                oAdapter.SelectCommand = oCommand;
                oAdapter.Fill(dt);

                int iRow = dt.Rows.Count;

                if (iRow > 0)
                {
                    btnLotNumber.DataSource = dt;
                    btnLotNumber.ValueMember = "LotNumber";
                    btnLotNumber.DisplayMember = "LotNumber";
                    btnLotNumber.Refresh();
                }

                oConnection.Close();

            }
            else
            {
                MessageBox.Show("Select item to continue!", "System Message");
                btnItemCode.Focus();
                return;
            }


            #endregion
        }
        
        private void btnRetrieve_Click(object sender, EventArgs e)
        {
            DevExpress.XtraSplashScreen.SplashScreenManager.ShowForm(typeof(WaitForm6));

            #region retrieve data
            SqlConnection oConnection = new SqlConnection(this.ConnectionString);
            oConnection.Open();

            SqlCommand oCommand = new SqlCommand();
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.CommandText = "SP_STOCKMOVEMENT";
            oCommand.Connection = oConnection;
            oCommand.Parameters.AddWithValue("@PC_DOCUMENT", btnDocument.Text);
            oCommand.Parameters.AddWithValue("@WHSECODE", cmbWhs.SelectedValue);
            oCommand.Parameters.AddWithValue("@ITEMCODE", btnItemCode.Text);
            oCommand.Parameters.AddWithValue("@LOTNUMBER", btnLotNumber.Text);
            oCommand.Parameters.AddWithValue("@ASOFDATE", Convert.ToDateTime(dtAsOf.EditValue));

            SqlDataAdapter oAdapter = new SqlDataAdapter();
            DataTable dt = new DataTable();

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(dt);

            int iRow = dt.Rows.Count;

            if (iRow > 0)
            {
                if (chartControl1.DataSource == null)
                {
                    chartControl1.DataSource = dt;

                    Series series2 = new Series("In Qty", ViewType.Bar);
                    chartControl1.Series.Add(series2);
                    series2.ArgumentDataMember = "Document";

                    series2.ValueDataMembers.AddRange(new string[] { "InQty" });

                    Series series1 = new Series("Out Qty", ViewType.Bar);
                    chartControl1.Series.Add(series1);
                    series1.ArgumentDataMember = "Document";

                    series1.ValueDataMembers.AddRange(new string[] { "OutQty" });
                    chartControl1.Refresh();
                }
                else
                {
                    chartControl1.Refresh();
                    chartControl1.Series.Clear();
                    chartControl1.DataSource = dt;
                    
                    Series series2 = new Series("In Qty", ViewType.Bar);
                    chartControl1.Series.Add(series2);
                    series2.ArgumentDataMember = "Document";

                    series2.ValueDataMembers.AddRange(new string[] { "InQty" });

                    Series series1 = new Series("Out Qty", ViewType.Bar);
                    chartControl1.Series.Add(series1);
                    series1.ArgumentDataMember = "Document";

                    series1.ValueDataMembers.AddRange(new string[] { "OutQty" });
                    chartControl1.Refresh();
                }
                
               


            }

            #endregion
            
            DevExpress.XtraSplashScreen.SplashScreenManager.CloseForm();
   
        }

        private void ProductMovement_Load(object sender, EventArgs e)
        {
            BindWarehouse();
        }

        private void btnLotNumber_DropDown(object sender, EventArgs e)
        {
            BindLotNumber();
        }

        private void btnItemCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            BRWS_P_ITEM oForm = new BRWS_P_ITEM();
            oForm.ConnectionString = this.ConnectionString;
            oForm.selectedWhs = cmbWhs.SelectedValue.ToString();
            oForm.ShowDialog();

            btnItemCode.Text = oForm.ItemSelected;
            txtDesc.Text = oForm.ItemSelected2;
        }

        private void btnDocument_DropDown(object sender, EventArgs e)
        {
            if (cmbWhs.Text != "" && cmbWhs.Text != null)
            {
                BindPhysicalCount();
            }
            else
            {
                MessageBox.Show("No warehouse found!", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        private void txtDesc_TextChanged(object sender, EventArgs e)
        {
            chartControl1.Series.Clear();
        }

        private void btnDocument_SelectedIndexChanged(object sender, EventArgs e)
        {
            chartControl1.Series.Clear();
        }
        
    }
}